import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import verifyToken from '@/middlewares/verifyToken'
import { Models } from '@/common/typings/modle'
import { command } from '@/server/mysql'
import { Success } from '@/core/HttpException'
import { addInsert, getFieldValues, getFields } from '@/common/utils/sql'
import { format } from '@/common/utils/date'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/valueSet`,
})
router.post('/add', verifyToken, async (ctx: Models.Ctx) => {
  const {
    setCode = '',
    setName = '',
    code = '',
    name = '',
    parentSetCode = '',
    parentSetName = '',
  } = ctx.request.body as any
  const auth = ctx.auth
  const fieldObj = {
    setCode,
    setName,
    code,
    name,
    parentSetCode,
    parentSetName,
  }
  const fields = `,created_at,updated_at,created_user_id, created_user_name,updated_user_id, updated_user_name`
  const newDate = format(new Date())
  const values = `,'${newDate}', '${newDate}', '${auth.id}', '${auth.userName}', '${auth.id}', '${auth.userName}'`
  const result = (
    await command(
      addInsert(
        `INSERT INTO system_value_set
 (${getFields(fieldObj)})
  values
   (${getFieldValues(fieldObj)}
   );`,
        fields,
        values,
      ),
      true,
    )
  ).results
  if (result.affectedRows === 1) {
    throw new Success({ data: result, msg: '新增成功' })
  }
})
export default router
